package sim;

import sim.misc.Holder;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;

public class Image
{
	SpriteBatch batch;
	Texture texture;
	TextureRegion tr;
	public float rotation;
	float width;
	float height;
	float centerX;
	float centerY;
	float scaleX;
	float scaleY;

	public Image(String path)
	{
		rotation = 0;
		batch = Holder.b;
		
		
		
		texture = new Texture(Gdx.files.internal(path));
		texture = Holder.getTexture(texture, path);
		texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		
	
		
		
		tr = new TextureRegion(texture);
		height = texture.getHeight();
		width = texture.getWidth();
		
		centerX = width / 2;
		centerY = height / 2;
		scaleX = 1;
		scaleY = 1;
		

	}
	
	public Image(String path, float width, float height)
	{
		this(path);
		setSize(width, height);
	}

	public void draw(float x, float y)
	{

		Holder.b.draw(tr, x, y, centerX, centerY, width, height, scaleX, scaleY,
				rotation);

	}
	
	public void draw(Vector2 pos)
	{

		Holder.b.draw(tr, pos.x, pos.y, centerX, centerY, width, height, scaleX, scaleY,
				rotation);

	}

	/**
	 * Sets the size of image in world coordinates. This is NOT in pixel
	 * coordinates. <br />
	 * This also resets the centre of rotation to the centre of the image.
	 * @param width
	 *            - The new width of the image in world coordinates.
	 * @param height
	 *            - The new height of the image in world coordinates.
	 */

	public void setSize(float width, float height)
	{
		this.height = height;
		this.width = width;
		
		centerX = width / 2;
		centerY = height / 2;
	}

	public void rotate(float angle)
	{
		
		if(rotation > 360 || rotation < -360)
		{
			rotation = rotation - 360;
		}
			
		rotation += angle;
	}
	
	public void setRotation(float angle)
	{
		
		
		this.rotation = angle;
	}
	
	/**
	 * Rotates the image to look at the direction of the mouse.
	 * Assumes the bit which looks at the mouse is at the top centre of the image.
	 * 
	 * @param x - The position of the image on the x axis.
	 * @param y - The position of the image on the y axis.
	 * @param camera - The current viewing camera.
	 */
	public void lookAtMouse(Camera camera, float x, float y)
	{
		
		Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
		
		
		
		camera.unproject(mousePos);
		mousePos.x -= x;
		mousePos.y -= y;
		
		
		
		System.out.println(mousePos.x + "  " + mousePos.y);
		
		
		double angle = Math.atan2(mousePos.x, mousePos.y);
		angle = Math.toDegrees(angle);
		angle *= -1;
		setRotation((float) angle);
		
		
	}

	public float getWidth()
	{
		return width;
	}

	public void setWorldWidth(float worldWidth)
	{
		this.width = worldWidth;
	}

	public float getHeight()
	{
		return height;
	}

	public void setHeight(float worldHeight)
	{
		this.height = worldHeight;
	}

}
